/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package lesson10;

import java.util.Iterator;
import java.util.Objects;

/**
 *
 * @author netcracker
 */
public class ProtectedVector implements IVector {

    IVector v;
    private boolean edited = false;

    ProtectedVector(IVector vector) {
        v = vector;
    }

    @Override
    public void add(double o) {
        System.out.println("Access Denied");
    }

    @Override
    public void delete(int index) {
        v.delete(index);
    }

    @Override
    public Double get(int id) {
        return v.get(id);
    }

    @Override
    public void set(double value, int position) {
        System.out.println("Access Denied");
    }

    @Override
    public void fill(double[] mass) {
        if (this.getLength() == mass.length) {
            for (int i = 0; i < this.getLength(); i++) {
                v.add(mass[i]);
            }
        }
    }

    @Override
    public void fill(IVector myVector) {
        if (this.getLength() == myVector.getLength()) {
            for (int i = 0; i < this.getLength(); i++) {
                v.add(myVector.get(i));
            }
        }
    }

    @Override
    public boolean isSame(IVector myVector) {
        if (this.v.getLength() == myVector.getLength()) {
            for (int i = 0; i < this.v.getLength(); i++) {
                boolean b = this.v.get(i) == myVector.get(i);
                if (b == false) {
                    return false;
                } else {
                    return true;
                }
            }
        }
        return false;
    }

    @Override
    public int getLength() {
        return v.getLength();
    }

    @Override
    public void multiply(double f) {
        for (int i = 0; i < this.v.getLength(); i++) {
            this.v.set(this.v.get(i) * f, i);
        }
    }

    @Override
    public void plus(IVector myVector) {
        if (this.v.getLength() == myVector.getLength()) {

            for (int i = 0; i < this.v.getLength(); i++) {
                this.v.set(this.v.get(i) + myVector.get(i), i);
            }
        }
    }

    @Override
    public void sort() {
        for (int i = this.v.getLength() - 1; i > 0; i--) {
            for (int j = 0; j < i; j++) {
                if (this.v.get(j) > this.v.get(j + 1)) {
                    this.swap(j, j + 1);
                }
            }
        }
    }

    @Override
    public void swap(int i, int j) {
        Double t = this.v.get(i);
        this.v.set(this.v.get(j), i);
        this.v.set(t, j);
    }

    @Override
    public Iterator<Double> iterator() {
        return new Iterator<Double>() {
            int marker = 0;

            @Override
            public boolean hasNext() {
                if (v.getLength() > marker) {
                    return true;
                }
                return false;
            }

            @Override
            public Double next() {
                double result = v.get(marker);
                marker++;

                return result;
            }

            @Override
            public void remove() {
                throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
            }
        };
    }

    @Override
    public ProtectedVector clone() throws CloneNotSupportedException {
        ProtectedVector pv = (ProtectedVector) super.clone();
        pv.v = this.v.clone();
        return pv;

    }

    @Override
    public String toString() {
        return v.toString();
    }

    @Override
    public int hashCode() {
        return v.hashCode();
    }

    @Override
    public boolean equals(Object obj) {
        return v.equals(obj);
    }

    @Override
    public boolean isEdited() {
        return edited;
    }

    @Override
    public void setEdited(boolean b) {
        edited = b;
    }
}
